﻿  require 'solarpv_tool/tool/solarpv_layoutComponent_action.rb'
  require 'solarpv_tool/tool/solarpv_layoutBracket_action.rb'
  require 'solarpv_tool/tool/screenshot_action.rb'
  require 'solarpv_tool/tool/synchronization_action.rb'
  require 'solarpv_tool/tool/open_action.rb'
module SolarpvTool
         $dialog_state = 1
         class SolarpvDialog
             def activate
                 #$dialog_state = 2
             end
             def show_dialog()
                    puts #{$dialog_state}
                    if $dialog_state == 1
                              properties = { dialog_title: "SolarPV-3D",
                                         preferences_key: "su.solarpv.top",
                                         scrollable: true,
                                         resizable: true,
                                         width: 1200,
                                         height: 500,
                                         min_width: 1200,
                                         min_height: 500,
                                         style: UI::HtmlDialog::STYLE_DIALOG
                                         }
                              dialog = UI::HtmlDialog.new(properties)
                              dialog.set_url("http://su.solarpv.top")
                              dialog.center
                              dialog.show
                              $dialog_state = 2
                              #打开项目skp文件
                              dialog.add_action_callback('open_action'){ |_dlg, projectId|
                                              #UI.messagebox(projectId)
                                              open_function(dialog,projectId);
                                  }

                              #组件布置工程
                              dialog.add_action_callback('layout_component_action'){ |_dlg, width,length,thick,power,dip,azimuth,layoutType,componentMargin,componentSpacing,stackNum,stackMargin,overhaulPortraitNum,overhaulTranNum,overhaulTranWidth,overhaulLongitudinalWidth,outMargin,bottomSpacing,componentNum,isAlgin,valve,fzaArray|
                                    puts "----  程序开始---------"+Time.now.to_s
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
require 'matrix'
if status = Sketchup.active_model.selection[0]
def par(edge_x0,edge_y0,delta,f)
    num=edge_y0.length
    edge_point=Array.new(num){Array.new(2,0)}
    n=Array.new(num){Array.new(2,0)}
    k=[]
    b=[]
    num.times do |i|
        ax=edge_x0[i]
        ay=edge_y0[i]
        next1=(i+1)%num
        bx=edge_x0[next1]
        by=edge_y0[next1]
        l=Math.sqrt((ax-bx)**2+(ay-by)**2)
        n[i][0]=(bx-ax)/l
        n[i][1]=(by-ay)/l
        k[i]=(by-ay)/(bx-ax)
        if k[i]==1.0/0.0
            b[i]=ax
        elsif k[i]==-1.0/0.0
            b[i]=ax
        else
            b[i]=ay-k[i]*ax
        end
    end
    key=edge_y0.max
    a=edge_y0.index(key) 
    head=(a-1)%num
    n1=[]
    n2=[]
    n1=[n[a][0],n[a][1]]
    n2=[-n[head][0],-n[head][1]]
    n1_L=Math.sqrt(n1[0]**2+n1[1]**2)
    n2_L=Math.sqrt(n2[0]**2+n2[1]**2)
    cos_theta=(n1[0]*n2[0]+n1[1]*n2[1])/(n1_L*n2_L)
    sin_theta=Math.sqrt(1-cos_theta**2)
    l1=delta/sin_theta
    n1.length.times do |i|
        n1[i]=f*n1[i]*l1
    end
    n2.length.times do |i|
        n2[i]=f*n2[i]*l1
    end
    edge_point[a][0]=edge_x0[a]+n1[0]+n2[0]
    edge_point[a][1]=edge_y0[a]+n1[1]+n2[1]
    (num-1).times do |i|
        a=(a+1)%num
        head=(a-1)%num
        n1=[n[a][0],n[a][1]]
        n2=[-n[head][0],-n[head][1]]
        cos_theta=(n1[0]*n2[0]+n1[1]*n2[1])/(n1_L*n2_L)
        sin_theta=Math.sqrt(1-cos_theta**2)
        l2=delta/sin_theta
        n1.length.times do |i|
        n1[i]=n1[i]*l2
        end
        n2.length.times do |i|
        n2[i]=n2[i]*l2
        end
        x0=edge_point[head][0]
        y0=edge_point[head][1]
        x=edge_x0[a]+n1[0]+n2[0]
        y=edge_y0[a]+n1[1]+n2[1]
        key=(y-y0)/(x-x0)
        k_key=k[head]
        b_key=b[head]
        if k_key==1.0/0.0
            t=(x-b_key)*(x0-b_key)
            if t>0
            else
                x=edge_x0[a]-n1[0]-n2[0]
                y=edge_y0[a]-n1[1]-n2[1]
            end
        elsif k_key==-1.0/0.0
            t=(x-b_key)*(x0-b_key)
            if t>0
            else
                x=edge_x0[a]-n1[0]-n2[0]
                y=edge_y0[a]-n1[1]-n2[1]
            end
        else
            aa=k_key*x+b_key
            bb=k_key*x0+b_key
            t=(aa-y)*(bb-y0)
            if t>0
            else
                x=edge_x0[a]-n1[0]-n2[0]
                y=edge_y0[a]-n1[1]-n2[1]
            end
        end
        edge_point[a][0]=x
        edge_point[a][1]=y
    end
    return edge_point
end
def get_point(y,num,edge)
    x=[]
    n=0
    k=0
    b=1
    y_max=2
    y_min=3
    num.times do |i|
        head=(i-1+num)%num
        tail=(i+1+num)%num
        if ((y<=edge[i][y_max])&&(y>=edge[i][y_min]))
            if edge[i][k]==1.0/0.0
                x[n]=edge[i][b]
                if n==0
                  n=n+1
                  next
                end
                n=n+1
            elsif edge[i][k]==-1.0/0.0
                x[n]=edge[i][b]
                if n==0
                    n=n+1
                    next
                end
                n=n+1            
            elsif edge[i][k]==0
                if (edge[head][y_max]==edge[tail][y_max])||(edge[head][y_min]==edge[tail][y_min])
                else
                  x[n]=edge[i][b]
                  n=n+1
                end
            else
                x[n]=(y-edge[i][b])/edge[i][k]
                if n==0
                  n=n+1
                  next
                end
                n=n+1
            end
        end
    end
   return x
end
def put_solargai(x,solar_long,big,small,number)
    s_x=[]
    key=x.length/2
    num=0
key.times do |i|
    k=1
    left=x[2*i]
    right=x[2*i+1]
    s_x=s_x+[left]
    num=num+1
    while 
    if k==number
        delta=big
        k=1
    else
        delta=small
        k=k+1
    end
    left=left+solar_long+delta
    if left+solar_long<right
        s_x=s_x+[left]
        num=num+1
    else
        break
    end
    end
end
return s_x
end
def find(key,x)
    n=0
    i=0
    num=x.length
    until i>num-1
    if key<x.first
        n=0
        break
    end
    if key>=x.last
       n=num
       break
    end
    if key>=x[i]&&key<x[i+1]
        n=i+1
    end
    if n>0
       break
    end
    i=i+1
    end
   return n
end
def co(x1,x2)
    x=[]
    if x1[1]<=x2[0]
        x=[]
    elsif x2[1]<=x1[0]
        x=[]
    else
        t=x1+x2
        t=t.sort
        x=[t[1],t[2]]
    end
end
def co2(x1,x2,f)
    k1=x1.length/2
    k2=x2.length/2
    x=[]
    if f==1
        k1.times do |i|
            k2.times do |n|
                t1=[x1[2*i],x1[2*i+1]]
                t2=[x2[2*n],x2[2*n+1]]
                t=co(t1,t2)
                x=x+t
                x=x.uniq.sort!
            end
        end
    else
        k1.times do |i|
            if k1==0
                x=x2
                break
            end
            if k2==0
                x2=x1
                break
            end
        n1=find(x1[2*i],x2)
        s1=(n1/2).floor
        r1=n1%2
        n2=find(x1[2*i+1],x2)
        s2=(n2/2).floor
        r2=n2%2
            if (r1==0)&&(r2==0)
                if s1==s2
                  x2=[x1[2*i],x1[2*i+1]]+x2
                  x2=x2.uniq.sort!
                else
                  x2[2*s1..2*s2-1]=[x1[2*i],x1[2*i+1]]
                  x2=x2.uniq.sort!
                end
            elsif r1*r2==1
                if s1==s2
                else
                  x2[2*s1+1..2*s2]=[x1[2*i],x1[2*i+1]]
                end
            else
                if r1==0
                  x2[2*s1..2*s2]=x1[2*i]
                else
                  x2[2*s1+1..2*s2-1]=x1[2*i+1]
                end
            end
        end
        x=x2
    end
   return x
end
def hide_instance(fzaArray,valve,inst,num)
num.times do |i|
  if fzaArray[i]>=valve
  else
  inst[i].visible=false
  end
end
end
def save_panel1(length,width,thick,stackNum,stackMargin)
mod = Sketchup.active_model
list = mod.definitions    
mat=mod.materials
comp_def = list.add "zujian"
comp_def.description = "test_components"
ent = comp_def.entities 
if stackNum==0
   pt=[[0,0,0],[length,0,0],[length,width,0],[0,width,0]]
   pt1=[[0,0,thick],[length,0,thick],[length,width,thick],[0,width,thick]]
   face = ent.add_face pt
   face.reverse!
   face.pushpull thick
   face1 = ent.add_face pt1
   mat1=mat.add "Mat1"
   mat1.texture = Sketchup.find_support_file "test1.png", "Plugins"
   face1.material = mat1
   save_path = Sketchup.find_support_file "components", ""
   comp_def.save_as(save_path + "/panel.skp")
else
  stackNum.times do |i|
    pt=[[0,0+(width+stackMargin)*i,0],[length,0+(width+stackMargin)*i,0],[length,width+(width+stackMargin)*i,0],[0,width+(width+stackMargin)*i,0]]
    pt1=[[0,0+(width+stackMargin)*i,thick],[length,0+(width+stackMargin)*i,thick],[length,width+(width+stackMargin)*i,thick],[0,width+(width+stackMargin)*i,thick]]
    face = ent.add_face pt
    face.reverse!
    face.pushpull thick
    face1 = ent.add_face pt1
    mat1=mat.add "Mat1"
    mat1.texture = Sketchup.find_support_file "test1.png", "Plugins"
    face1.material = mat1
  end
  save_path = Sketchup.find_support_file "components", ""
  comp_def.save_as(save_path + "/panel.skp")
end
end
stackNum=stackNum.to_i
stackMargin=stackMargin.cm
componentMargin=componentMargin.cm
componentSpacing=componentSpacing.cm
overhaulTranWidth=overhaulTranWidth.cm
overhaulLongitudinalWidth=overhaulLongitudinalWidth.cm
outMargin=outMargin*0.01
bottomSpacing=bottomSpacing.cm
if layoutType==1
  long=width.m
  wide=length.m
else
  long=length.m
  wide=width.m
end
thick=0.04.m
#------------------baijb--------------------------
p save_panel1(long,wide,thick,stackNum,stackMargin)
def_list=mod.definitions
temp_com = def_list['SolarPV123']
if temp_com
p Sketchup.active_model.definitions['SolarPV123'].count_instances
   def_list.remove(temp_com)
end
path=Sketchup.find_support_file "panel.skp","components"
test_panel= def_list.load path
test_panel.name="SolarPV123"
#------------------baijb--------------------------
if stackNum==0
  solar_long=long
  solar_wide=wide
else
  solar_long=long
  solar_wide=(wide+(wide+stackMargin)*(stackNum-1))
end
obj = Sketchup.active_model.selection[0]
inner_loops = obj.loops - [obj.outer_loop]	
# p 'outer loop'
outer_array=[]
outer_array=obj.outer_loop.vertices.collect{|v| v.position}	
# In your original code array.collect returns an array of vertex positions
# but you aren't storing that data anywhere
#p inner_loops.each{|l| l.vertices.collect{|v| v.position}}
# right up here -----  ^
# so let's create a place to store our arrays of vertex positions
inner_array = [] 
# iterate the loops and push the vertex positions array on to the inner_array
inner_loops.each{|l|
  inner_array << l.vertices.collect{|v| v.position}
}
face = Sketchup.active_model.selection[0]
vector_face_norm = face.normal
zenith1=vector_face_norm.angle_between Z_AXIS
zenith1=Math::PI-zenith1
vector1=vector_face_norm 
vector1[2]=0
azimuth1=vector1.angle_between Y_AXIS
if vector1[0]>0
azimuth1=-azimuth1
end
if zenith1==0
azimuth1=0
end
fxj=azimuth1.radians
fxj=fxj.round(1)
qj=zenith1.radians
qj=qj.round(1)
#chan_fq函数定义
theta1=azimuth
if qj>0
theta1=0
end
def chan_fq(edge,num,fxj,qj)
    c_q=[[1,0,0],[0,Math.cos(Math::PI*qj/180),-Math.sin(Math::PI*qj/180)],[0,Math.sin(Math::PI*qj/180),Math.cos(Math::PI*qj/180)]]
    c_f=[[Math.cos(Math::PI*fxj/180),-Math.sin(Math::PI*fxj/180),0],[Math.sin(Math::PI*fxj/180),Math.cos(Math::PI*fxj/180),0],[0,0,1]]
    c=Matrix[c_f[0],c_f[1],c_f[2]]*Matrix[c_q[0],c_q[1],c_q[2]]
    edge_0=Matrix[*edge]
    edge1=edge_0*c
    return edge1
end
#chan_theta函数定义
def chan_theta(edge,num,theta)
    c_t=[[Math.cos(Math::PI*theta/180),-Math.sin(Math::PI*theta/180),0],[Math.sin(Math::PI*theta/180),Math.cos(Math::PI*theta/180),0],[0,0,1]]
    edge_0=Matrix[*edge]
    edge2=edge_0*Matrix[c_t[0],c_t[1],c_t[2]]
    return edge2
end
num=outer_array.length
edge_0=[]
num.times do |i|
edge_0=edge_0+[[outer_array[i][0],outer_array[i][1],outer_array[i][2]]]
end
e_num=edge_0.length
edge_1=chan_fq(edge_0,e_num,fxj,qj)
edge_2=chan_theta(edge_1,e_num,theta1)
edge_3=[]
e_num.times do |i|
edge_3=edge_3+[[edge_2.element(i,0),edge_2.element(i,1),edge_2.element(i,2)]]
end
c_q=[[1,0,0],[0,Math.cos(Math::PI*qj/180),-Math.sin(Math::PI*qj/180)],[0,Math.sin(Math::PI*qj/180),Math.cos(Math::PI*qj/180)]]
c_f=[[Math.cos(Math::PI*fxj/180),-Math.sin(Math::PI*fxj/180),0],[Math.sin(Math::PI*fxj/180),Math.cos(Math::PI*fxj/180),0],[0,0,1]]
c_t=[[Math.cos(Math::PI*theta1/180),-Math.sin(Math::PI*theta1/180),0],[Math.sin(Math::PI*theta1/180),Math.cos(Math::PI*theta1/180),0],[0,0,1]]
#fxj,qj为斜面的倾角，theta为斜面和直线经过fxj和qj变化（chan_fq）之后，直线和y轴的夹角
c = Matrix[c_f[0],c_f[1],c_f[2]]*Matrix[c_q[0],c_q[1],c_q[2]]*Matrix[c_t[0],c_t[1],c_t[2]]
f=1
edge_x0=[]
edge_y0=[]
z1=edge_3[0].z
num.times do |i|
   edge_x0[i]=edge_3[i].x
   edge_x0[i]=edge_x0[i].to_m
   edge_y0[i]=edge_3[i].y
   edge_y0[i]=edge_y0[i].to_m
end
point0=par(edge_x0,edge_y0,outMargin,f)
num.times do |i|
	edge_x0[i]=point0[i][0]
	edge_x0[i]=edge_x0[i].m
	edge_y0[i]=point0[i][1]
	edge_y0[i]=edge_y0[i].m
end
k=0
b=1
y_max=2
y_min=3
edge = Array.new(num){ Array.new(4,0)}
num.times do |i|
  s=(i+1)%num
  edge[i][k]=(edge_y0[s]- edge_y0[i])/(edge_x0[s]- edge_x0[i])
  if  edge[i][k]==0
    edge[i][b]=edge_x0[i] 
  elsif edge[i][k]==1.0/0.0
    edge[i][b]=edge_x0[i]
  elsif edge[i][k]==-1.0/0.0
    edge[i][b]=edge_x0[i]
  else
    edge[i][b]=edge_y0[i]-edge[i][k]*edge_x0[i]
  end
  if edge_y0[i]>=edge_y0[s]
    edge[i][y_max]=edge_y0[i]
    edge[i][y_min]=edge_y0[s]
  else
    edge[i][y_max]=edge_y0[s]
    edge[i][y_min]=edge_y0[i]
  end
end
block_n=inner_array.length
block_num=[]
num1=0
block_n.times do |i|
   block_num[i]=inner_array[i].length
   num1=num1+inner_array[i].length
end
inner_array1=[]
block_n.times do |i|
block_0=[]
block_num[i].times do |j|
  block_0=block_0+[[inner_array[i][j].x,inner_array[i][j].y,inner_array[i][j].z]]
end
b_num=block_0.length
block_1=chan_fq(block_0,b_num,fxj,qj)
block_2=chan_theta(block_1,b_num,theta1)
block_3=[]
b_num.times do |k|
  block_3=block_3+[[block_2.element(k,0),block_2.element(k,1),block_2.element(k,2)]]
end
inner_array1=inner_array1+[block_3]
end
block_x0 = []
block_y0 = []
block_x = []
block_y = []
block_n.times do |i|
   block_num[i].times do |j|
       block_x0[j] =inner_array1[i][j].x
       block_y0[j] =inner_array1[i][j].y
    end
    block_x=block_x+block_x0
    block_y=block_y+block_y0
end
num1.times do |i|
	block_x[i]=block_x[i].to_m
	block_y[i]=block_y[i].to_m
end
ip=0
point1=[]
block_n.times do |i|
ip=ip+block_num[i]
point11=par(block_x[ip-block_num[i]..ip-1],block_y[ip-block_num[i]..ip-1],outMargin,-f)
point1=point1+point11
end
num1.times do |i|
	block_x[i]=point1[i][0]
	block_x[i]=block_x[i].m
	block_y[i]=point1[i][1]
	block_y[i]=block_y[i].m
end
k=0
b=1
y_max=2
y_min=3
block1 =[]
ip=0
x1=[]
y1=[]
block_n.times do |i|
  block = Array.new(block_num[i]){Array.new(4)}
  ip=ip+block_num[i]
  x1=block_x[ip-block_num[i]..ip-1]
  y1=block_y[ip-block_num[i]..ip-1]
  block_num[i].times do |j|
  s=(j+1)%block_num[i]
  block[j][k]=(y1[s]- y1[j])/(x1[s]- x1[j])
  if  block[j][k]==0
    block[j][b]=x1[j] 
  elsif block[j][k]==1.0/0.0
    block[j][b]=x1[j]
  elsif block[j][k]==-1.0/0.0
    block[j][b]=x1[j]
  else
    block[j][b]=y1[j]-block[j][k]*x1[j]
  end
  if y1[j]>=y1[s]
    block[j][y_max]=y1[j]
    block[j][y_min]=y1[s]
  else
    block[j][y_max]=y1[s]
    block[j][y_min]=y1[j]
  end
  end 
  x1=[]
  y1=[]
  block1=block1+block
end
k=0
b=1
y_max=2
y_min=3
way=2
final_num = 0
final_point = []
min_x=edge_x0.min
max_x=edge_x0.max
min_y=edge_y0.min
max_y=edge_y0.max
inst=[]
inst_array=[]
beta=(qj+dip)/180.0
theta=(fxj+theta1)/180.0
if isAlgin==false
  #对齐
  final_num0=0
  final_point0=[]
  xx=[]
  i=min_x
  n=0
  k=1
  until i>max_x
    if k==overhaulPortraitNum
      delta_x=overhaulLongitudinalWidth
      k=1
    else
      delta_x=componentMargin
      k=k+1
    end
    if i+solar_long<max_x
      xx[n]=i
      n=n+1
    end
    i=i+delta_x+solar_long
  end
  xx.length.times do |i|
      xx[i]=xx[i].round(1)
  end
  k=1
  y=min_y
  num_end=0
  until y>max_y
    ex1=get_point(y,num,edge)
	  ex1.length.times do |i|
	    ex1[i]=ex1[i].round(1)
	  end
    ex1=ex1.uniq.sort!
    ex2=get_point(y+solar_wide*Math.cos(Math::PI*(dip/180.0)),num,edge)
	  ex2.length.times do |i|
      ex2[i]=ex2[i].round(1)
    end
    ex2=ex2.uniq.sort!
    ex=co2(ex1,ex2,1)
    final_bx=[]
    ip=0
    block_n.times do |i|
      ip=ip+block_num[i]
      bx1=get_point(y,block_num[i],block1[ip-block_num[i]..ip-1])
      bx2=get_point(y+solar_wide*Math.cos(Math::PI*beta),block_num[i],block1[ip-block_num[i]..ip-1])
      bx1.length.times do |j|
	      bx1[j]=bx1[j].round(1)
      end
      bx1=bx1.uniq.sort!
      bx2.length.times do |j|
	      bx2[j]=bx2[j].round(1)
      end
      bx2=bx2.uniq.sort!
      bx=co2(bx1,bx2,-1)
      final_bx=final_bx+bx
      final_bx=final_bx.uniq.sort!
    end
    x=ex+final_bx
    x=x.uniq.sort!
    num_xx=xx.length
    put_i=0
    get_x=[]
    n=0
    num_xx.times do |i|
      if x[2*put_i]==nil
            break
        elsif xx[i]>=x[2*put_i]
          if xx[i]+solar_long<x[2*put_i+1]
            get_x[n]=xx[i]
            n=n+1
            final_num=final_num+1
          else
            put_i=put_i+1
          end
        end
        if put_i>x.length/2
          break
        end
    end
    put_num=get_x.length
    put_num.times do |i|
      inst_array=inst_array+[[get_x[i],y]]
    end
    num_end=num_end+put_num
    if k==overhaulTranNum
      k=1
      y=y+overhaulTranWidth+solar_wide*Math.cos(Math::PI*(dip/180.0))
    else
      k=k+1
      y=y+componentSpacing+solar_wide*Math.cos(Math::PI*(dip/180.0))
    end
  end
  zujian_num=inst_array.length
  if zujian_num>=componentNum
    jisuan_num=componentNum
  else
    jisuan_num=zujian_num
  end
  jisuan_num.times do |i|
      test_point1=Matrix[[inst_array[i][0],inst_array[i][1],z1+bottomSpacing]]*(c.inverse)
      test_point2=[test_point1.element(0,0),test_point1.element(0,1),test_point1.element(0,2)]
      t_tran=Geom::Transformation.translation [test_point2[0],test_point2[1],test_point2[2]]
      t_rota = Geom::Transformation.new [0,0,0],[1,0,0],(dip+qj).degrees
      t_rota1 = Geom::Transformation.new [0,0,0],[0,0,1],(theta1+fxj).degrees
      t=t_tran*t_rota1*t_rota
      inst1=ent.add_instance test_panel,t
     inst=inst+[inst1] 
   end
   num_end=jisuan_num
else
  #非对齐
  final_num0=0
  final_point0=[]
  y_k=1
  y=min_y
  num_end=0
  until y>max_y
    ex1=get_point(y,num,edge)
	  ex1.length.times do |i|
	    ex1[i]=ex1[i].round(1)
	  end
    ex1=ex1.uniq.sort!
    ex2=get_point(y+solar_wide*Math.cos(Math::PI*(dip/180.0)),num,edge)
	  ex2.length.times do |i|
      ex2[i]=ex2[i].round(1)
    end
    ex2=ex2.uniq.sort!
    ex=co2(ex1,ex2,1)
    final_bx=[]
    ip=0
    block_n.times do |i|
      ip=ip+block_num[i]
      bx1=get_point(y,block_num[i],block1[ip-block_num[i]..ip-1])
      bx2=get_point(y+solar_wide,block_num[i],block1[ip-block_num[i]..ip-1])
      bx1.length.times do |j|
	      bx1[j]=bx1[j].round(1)
      end
      bx1=bx1.uniq.sort!
      bx2.length.times do |j|
	      bx2[j]=bx2[j].round(1)
      end
      bx2=bx2.uniq.sort!
      bx=co2(bx1,bx2,-1)
      final_bx=final_bx+bx
      final_bx=final_bx.uniq.sort!
    end
    x=ex+final_bx
    x=x.uniq.sort!
    q=put_solargai(x,solar_long,overhaulLongitudinalWidth,componentMargin,overhaulPortraitNum)
    put_num=q.length
     put_num.times do |i|
     inst_array=inst_array+[[q[i],y]]
   end
   num_end=num_end+put_num
  if y_k==overhaulTranNum
      y_k=1
      y=y+overhaulTranWidth+solar_wide*Math.cos(Math::PI*(dip/180.0))
    else
      y_k=y_k+1
      y=y+componentSpacing+solar_wide*Math.cos(Math::PI*(dip/180.0))
    end
  end
  zujian_num=inst_array.length
  if zujian_num>=componentNum
    jisuan_num=componentNum
  else
    jisuan_num=zujian_num
  end
  jisuan_num.times do |i|
      test_point1=Matrix[[inst_array[i][0],inst_array[i][1],z1+bottomSpacing]]*(c.inverse)
      test_point2=[test_point1.element(0,0),test_point1.element(0,1),test_point1.element(0,2)]
      t_tran=Geom::Transformation.translation [test_point2[0],test_point2[1],test_point2[2]]
      t_rota = Geom::Transformation.new [0,0,0],[1,0,0],(dip+qj).degrees
      t_rota1 = Geom::Transformation.new [0,0,0],[0,0,1],(theta1+fxj).degrees
      t=t_tran*t_rota1*t_rota
      inst1=ent.add_instance test_panel,t
     inst=inst+[inst1] 
   end
   num_end=jisuan_num
end
t1 = Time.utc(2020,1,17,7,15,0)          #7:15~16:45  10
t2 = Time.utc(2020,2,16,6,45,0)          #6:45~17:15  12
t3 = Time.utc(2020,3,16,6,8,0)            #6:08~17:52  12
t4 = Time.utc(2020,4,15,5,28,0)          #5:28~18:31  14
t5 = Time.utc(2020,5,15,4,54,0)          #4:54~19:06  16
t6 = Time.utc(2020,6,11,4,37,0)          #4:37~19:23  16
t7 = Time.utc(2020,7,17,4,44,0)          #4:44~19:16  16
t8 = Time.utc(2020,8,16,5,14,0)          #5:14~18:42  14
t9 = Time.utc(2020,9,15,5,53,0)          #5:53~18:07  14
t10 = Time.utc(2020,10,15,6,32,0)      #6:32~17:27  12
t11 = Time.utc(2020,11,14,7,7,0)        #7:07~16:53  10
t12 = Time.utc(2020,12,10,7,23,0)      #7:23~16:36  10
array=[t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12]
array_time=[]
array_time1=array_time2=array_time3=array_time4=array_time5=array_time6=array_time7=array_time8=array_time9=array_time10=array_time11=array_time12=[]
s1=s2=0
10.times do |i|
    array_time1=array_time1+[array[0]+60*60*i]
end
12.times do |i|
    array_time2=array_time2+[array[1]+60*60*i]
end
12.times do |i|
    array_time3=array_time3+[array[2]+60*60*i]
end
14.times do |i|
    array_time4=array_time4+[array[3]+60*60*i]
end
16.times do |i|
    array_time5=array_time5+[array[4]+60*60*i]
end
16.times do |i|
    array_time6=array_time6+[array[5]+60*60*i]
end
16.times do |i|
    array_time7=array_time7+[array[6]+60*60*i]
end
14.times do |i|
    array_time8=array_time8+[array[7]+60*60*i]
end
14.times do |i|
    array_time9=array_time9+[array[8]+60*60*i]
end
12.times do |i|
    array_time10=array_time10+[array[9]+60*60*i]
end
10.times do |i|
    array_time11=array_time11+[array[10]+60*60*i]
end
10.times do |i|
    array_time12=array_time12+[array[11]+60*60*i]
end
array_time=array_time1+array_time2+array_time3+array_time4+array_time5+array_time6+array_time7+array_time8+array_time9+array_time10+array_time11+array_time12
edge_time = Array.new(num_end){Array.new(12){Array.new(24,1)}}
result=[]
num_time=array_time.length
a1=b1=n1=0
num_time.times do |t|
info=mod.shadow_info
info["Displayshadows"]=true
info["DisplayOnGroundPlane"]=true
later=array_time[t]
info["ShadowTime"]=later
info["Latitude"]=31
info["Longitude"]=120
direction_towards_sun = info["SunDirection"]
# raytest Sends a ray into the Model and returns what, if anything, it touches
   num_end.times do |i|
   test_point1=Matrix[[inst_array[i][0],inst_array[i][1],z1+bottomSpacing]]*(c.inverse)
   test_point2=[test_point1.element(0,0),test_point1.element(0,1),test_point1.element(0,2)]
   ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*0+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*0,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*0+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*0,test_point2[2]+solar_wide*Math.sin(Math::PI*beta)/4*0+bottomSpacing+0.15.m), direction_towards_sun]
   ignore_hidden_geometry0 = false
   result00 = mod.raytest( ray, ignore_hidden_geometry0 )
   ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*4+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*4,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*4+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*4,test_point2[0]+solar_wide*Math.sin(Math::PI*beta)/4*4+bottomSpacing+0.15.m), direction_towards_sun]
   ignore_hidden_geometry1 = false
   result01 = mod.raytest( ray, ignore_hidden_geometry1 )
   if result00==nil
     n1=0
   elsif result01!=nil
     n1=4
   else
   ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*2+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*2,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*2+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*2,test_point2[2]+solar_wide*Math.sin(Math::PI*beta)/4*2+bottomSpacing+0.15.m), direction_towards_sun]
   ignore_hidden_geometry = false
   result1 = mod.raytest( ray, ignore_hidden_geometry )
   if result1==nil
     ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*1+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*1,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*1+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*1,test_point2[2]+solar_wide*Math.sin(Math::PI*beta)/4*1+bottomSpacing+0.15.m), direction_towards_sun]
     ignore_hidden_geometry = false
     result21 = mod.raytest( ray, ignore_hidden_geometry )
     if result21==nil
       n1=0 
     else
       n1=1
     end
    else
      ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*3+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*3,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*3+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*3,test_point2[2]+solar_wide*Math.sin(Math::PI*beta)/4*3+bottomSpacing+0.15.m), direction_towards_sun]
     ignore_hidden_geometry = false
     result22 = mod.raytest( ray, ignore_hidden_geometry )
     if result22==nil
       n1=2
     else
       n1=3    
     end
    end
    end
     p=n1/4.0
     p=p.round(2)
     result=result+[p]
     n1=0
   end
end
d=[10,12,12,14,16,16,16,14,14,12,10,10]
a=c=1
until c>d[0]
  until a>num_end
    edge_time[a-1][0][c-1+7]=result[(a-1)+num_end*(c-1)]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[1]
  until a>num_end
    edge_time[a-1][1][c-1+6]=result[(a-1)+num_end*(c-1)+num_end*10]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[2]
  until a>num_end
    edge_time[a-1][2][c-1+6]=result[(a-1)+num_end*(c-1)+num_end*22]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[3]
  until a>num_end
    edge_time[a-1][3][c-1+5]=result[(a-1)+num_end*(c-1)+num_end*34]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[4]
  until a>num_end
    edge_time[a-1][4][c-1+4]=result[(a-1)+num_end*(c-1)+num_end*48]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[5]
  until a>num_end
    edge_time[a-1][5][c-1+4]=result[(a-1)+num_end*(c-1)+num_end*64]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[6]
  until a>num_end
    edge_time[a-1][6][c-1+4]=result[(a-1)+num_end*(c-1)+num_end*80]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[7]
  until a>num_end
    edge_time[a-1][7][c-1+5]=result[(a-1)+num_end*(c-1)+num_end*96]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[8]
  until a>num_end
    edge_time[a-1][8][c-1+5]=result[(a-1)+num_end*(c-1)+num_end*110]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[9]
  until a>num_end
    edge_time[a-1][9][c-1+6]=result[(a-1)+num_end*(c-1)+num_end*124]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[10]
  until a>num_end
    edge_time[a-1][10][c-1+7]=result[(a-1)+num_end*(c-1)+num_end*136]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[11]
  until a>num_end
    edge_time[a-1][11][c-1+7]=result[(a-1)+num_end*(c-1)+num_end*146]
    a +=1
   end
   a=1
   c +=1
end
if stackNum==0
number_end=num_end
else
number_end=num_end*stackNum
end
  dialog.execute_script("layoutComPonentStats(200,#{number_end},#{edge_time})")
if fzaArray==[]
else
  p hide_instance(fzaArray,valve,inst,number_end)
end
else
  UI.messagebox("Please select a face")
  dialog.execute_script("layoutComPonentStats(500,'','')")
end


                                  }

                              #支架布置工程
                              dialog.add_action_callback('layout_bracket_action'){ |_dlg, width, length,thick,power,dip,azimuth,bracketLayoutType,bracketUpMargin,bracketDownMargin,bracketLeftMargin,bracketRightMargin,componentTranNum,componentMargin,stackNum,stackMargin,supplementTranNum,bracketSpacing,bracketLeftRightSpacing,overhaulTranNum,overhaulPortraitNum,overhaulTranWidth,overhaulLongitudinalWidth,outMargin,isAlgin,bottomSpacing,componentNum,minYearlyEffAvgRadiation,fzarray|
                                    #----------------------------------

mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
require 'matrix'
if status = Sketchup.active_model.selection[0]
height=100
thick_panel=4
thick_frame=5
thick_stone=40
thick_bracket1=5
thick_bracket2=40
thick_bracket3=5
stackNum=stackNum.to_i
componentTranNum=componentTranNum.to_i
stackNum1=stackNum
stackMargin1=stackMargin
stackNum2=componentTranNum
stackMargin2=componentMargin
componentMargin=bracketLeftRightSpacing
componentSpacing=bracketSpacing
valve=minYearlyEffAvgRadiation
componentNum=componentNum/(stackNum1*stackNum2)+1
#----------------------------------
bracketUpMargin=bracketUpMargin.cm
bracketDownMargin=bracketDownMargin.cm
bracketLeftMargin=bracketLeftMargin.cm
bracketRightMargin=bracketRightMargin.cm
thick_panel=thick_panel.cm
thick_frame=thick_frame.cm
thick_stone=thick_stone.cm
thick_bracket1=thick_bracket1.cm
thick_bracket2=thick_bracket2.cm
stackMargin1=stackMargin1.cm
stackMargin2=stackMargin2.cm
componentMargin=componentMargin.cm
componentSpacing=componentSpacing.cm
overhaulTranWidth=overhaulTranWidth.cm
overhaulLongitudinalWidth=overhaulLongitudinalWidth.cm
outMargin=outMargin*0.01
height=height.cm
bottomSpacing=bottomSpacing.cm
#----------------------------------
def par(edge_x0,edge_y0,delta,f)
    num=edge_y0.length
    edge_point=Array.new(num){Array.new(2,0)}
    n=Array.new(num){Array.new(2,0)}
    k=[]
    b=[]
    num.times do |i|
        ax=edge_x0[i]
        ay=edge_y0[i]
        next1=(i+1)%num
        bx=edge_x0[next1]
        by=edge_y0[next1]
        l=Math.sqrt((ax-bx)**2+(ay-by)**2)
        n[i][0]=(bx-ax)/l
        n[i][1]=(by-ay)/l
        k[i]=(by-ay)/(bx-ax)
        if k[i]==1.0/0.0
            b[i]=ax
        elsif k[i]==-1.0/0.0
            b[i]=ax
        else
            b[i]=ay-k[i]*ax
        end
    end
    key=edge_y0.max
    a=edge_y0.index(key) 
    head=(a-1)%num
    n1=[]
    n2=[]
    n1=[n[a][0],n[a][1]]
    n2=[-n[head][0],-n[head][1]]
    n1_L=Math.sqrt(n1[0]**2+n1[1]**2)
    n2_L=Math.sqrt(n2[0]**2+n2[1]**2)
    cos_theta=(n1[0]*n2[0]+n1[1]*n2[1])/(n1_L*n2_L)
    sin_theta=Math.sqrt(1-cos_theta**2)
    l1=delta/sin_theta
    n1.length.times do |i|
        n1[i]=f*n1[i]*l1
    end
    n2.length.times do |i|
        n2[i]=f*n2[i]*l1
    end
    edge_point[a][0]=edge_x0[a]+n1[0]+n2[0]
    edge_point[a][1]=edge_y0[a]+n1[1]+n2[1]
    (num-1).times do |i|
        a=(a+1)%num
        head=(a-1)%num
        n1=[n[a][0],n[a][1]]
        n2=[-n[head][0],-n[head][1]]
        cos_theta=(n1[0]*n2[0]+n1[1]*n2[1])/(n1_L*n2_L)
        sin_theta=Math.sqrt(1-cos_theta**2)
        l2=delta/sin_theta
        n1.length.times do |i|
        n1[i]=n1[i]*l2
        end
        n2.length.times do |i|
        n2[i]=n2[i]*l2
        end
        x0=edge_point[head][0]
        y0=edge_point[head][1]
        x=edge_x0[a]+n1[0]+n2[0]
        y=edge_y0[a]+n1[1]+n2[1]
        key=(y-y0)/(x-x0)
        k_key=k[head]
        b_key=b[head]
        if k_key==1.0/0.0
            t=(x-b_key)*(x0-b_key)
            if t>0
            else
                x=edge_x0[a]-n1[0]-n2[0]
                y=edge_y0[a]-n1[1]-n2[1]
            end
        elsif k_key==-1.0/0.0
            t=(x-b_key)*(x0-b_key)
            if t>0
            else
                x=edge_x0[a]-n1[0]-n2[0]
                y=edge_y0[a]-n1[1]-n2[1]
            end
        else
            aa=k_key*x+b_key
            bb=k_key*x0+b_key
            t=(aa-y)*(bb-y0)
            if t>0
            else
                x=edge_x0[a]-n1[0]-n2[0]
                y=edge_y0[a]-n1[1]-n2[1]
            end
        end
        edge_point[a][0]=x
        edge_point[a][1]=y
    end
    return edge_point
end
def get_point(y,num,edge)
    x=[]
    n=0
    k=0
    b=1
    y_max=2
    y_min=3
    num.times do |i|
        head=(i-1+num)%num
        tail=(i+1+num)%num
        if ((y<=edge[i][y_max])&&(y>=edge[i][y_min]))
            if edge[i][k]==1.0/0.0
                x[n]=edge[i][b]
                if n==0
                  n=n+1
                  next
                end
                n=n+1
            elsif edge[i][k]==-1.0/0.0
                x[n]=edge[i][b]
                if n==0
                    n=n+1
                    next
                end
                n=n+1            
            elsif edge[i][k]==0
                if (edge[head][y_max]==edge[tail][y_max])||(edge[head][y_min]==edge[tail][y_min])
                else
                  x[n]=edge[i][b]
                  n=n+1
                end
            else
                x[n]=(y-edge[i][b])/edge[i][k]
                if n==0
                  n=n+1
                  next
                end
                n=n+1
            end
        end
    end
   return x
end
def put_solargai(x,solar_long,big,small,number)
    s_x=[]
    key=x.length/2
    num=0
key.times do |i|
    k=1
    left=x[2*i]
    right=x[2*i+1]
    s_x=s_x+[left]
    num=num+1
    while 
    if k==number
        delta=big
        k=1
    else
        delta=small
        k=k+1
    end
    left=left+solar_long+delta
    if left+solar_long<right
        s_x=s_x+[left]
        num=num+1
    else
        break
    end
    end
end
return s_x
end
def find(key,x)
    n=0
    i=0
    num=x.length
    until i>num-1
    if key<x.first
        n=0
        break
    end
    if key>=x.last
       n=num
       break
    end
    if key>=x[i]&&key<x[i+1]
        n=i+1
    end
    if n>0
       break
    end
    i=i+1
    end
   return n
end
def co(x1,x2)
    x=[]
    if x1[1]<=x2[0]
        x=[]
    elsif x2[1]<=x1[0]
        x=[]
    else
        t=x1+x2
        t=t.sort
        x=[t[1],t[2]]
    end
end
def co2(x1,x2,f)
    k1=x1.length/2
    k2=x2.length/2
    x=[]
    if f==1
        k1.times do |i|
            k2.times do |n|
                t1=[x1[2*i],x1[2*i+1]]
                t2=[x2[2*n],x2[2*n+1]]
                t=co(t1,t2)
                x=x+t
                x=x.uniq.sort!
            end
        end
    else
        k1.times do |i|
            if k1==0
                x=x2
                break
            end
            if k2==0
                x2=x1
                break
            end
        n1=find(x1[2*i],x2)
        s1=(n1/2).floor
        r1=n1%2
        n2=find(x1[2*i+1],x2)
        s2=(n2/2).floor
        r2=n2%2
            if (r1==0)&&(r2==0)
                if s1==s2
                  x2=[x1[2*i],x1[2*i+1]]+x2
                  x2=x2.uniq.sort!
                else
                  x2[2*s1..2*s2-1]=[x1[2*i],x1[2*i+1]]
                  x2=x2.uniq.sort!
                end
            elsif r1*r2==1
                if s1==s2
                else
                  x2[2*s1+1..2*s2]=[x1[2*i],x1[2*i+1]]
                end
            else
                if r1==0
                  x2[2*s1..2*s2]=x1[2*i]
                else
                  x2[2*s1+1..2*s2-1]=x1[2*i+1]
                end
            end
        end
        x=x2
    end
   return x
end
def hide_instance(fzaArray,valve,inst,num)
num.times do |i|
  if fzaArray[i]>=valve
  else
  inst[i].visible=false
  end
end
end
def save_panel(length,width,thick)
mod = Sketchup.active_model
list = mod.definitions    
mat=mod.materials
comp_def = list.add "zujian"
comp_def.description = "test_components"
ent = comp_def.entities 
   pt=[[0,0,0],[length,0,0],[length,width,0],[0,width,0]]
   pt1=[[0,0,thick],[length,0,thick],[length,width,thick],[0,width,thick]]
   face = ent.add_face pt
   face.reverse!
   face.pushpull thick
   face1 = ent.add_face pt1
   mat1=mat.add "Mat1"
   mat1.texture = Sketchup.find_support_file "test1.png", "Plugins/solarpv_tool"
   face1.material = mat1
   save_path = Sketchup.find_support_file "components", ""
   comp_def.save_as(save_path + "/panel.skp")
end
def save_frame(length,width,thick,stackNum1,stackMargin1,stackNum2,stackMargin2)
mod = Sketchup.active_model
list = mod.definitions    
mat=mod.materials
comp_def = list.add "kuangjia"
comp_def.description = "test_frame"
ent = comp_def.entities 
path=Sketchup.find_support_file "panel.skp","components"
def_list=mod.definitions
test_panel= def_list.load path
stackNum1.times do |i|
  pt1=[[0-10.cm,(width+stackMargin1)*i,thick],[length*stackNum2+stackMargin2*(stackNum2-1)+10.cm,(width+stackMargin1)*i,thick],[length*stackNum2+stackMargin2*(stackNum2-1)+10.cm,(width+stackMargin1)*i+thick,thick],[0-10.cm,(width+stackMargin1)*i+thick,thick]]
  pt2=[[0-10.cm,(width+stackMargin1)*i+width-thick,thick],[length*stackNum2+stackMargin2*(stackNum2-1)+10.cm,(width+stackMargin1)*i+width-thick,thick],[length*stackNum2+stackMargin2*(stackNum2-1)+10.cm,(width+stackMargin1)*i+width,thick],[0-10.cm,(width+stackMargin1)*i+width,thick]]
  face1 = ent.add_face pt1
  face1.pushpull thick
  face2 = ent.add_face pt2
  face2.pushpull thick
end
pt3=[[0,0,0],[thick,0,0],[thick,width*stackNum1+stackMargin1*(stackNum1-1),0],[0,width*stackNum1+stackMargin1*(stackNum1-1),0]]
pt4=[[length*stackNum2+stackMargin2*(stackNum2-1)-thick,0,0],[length*stackNum2+stackMargin2*(stackNum2-1),0,0],[length*stackNum2+stackMargin2*(stackNum2-1),width*stackNum1+stackMargin1*(stackNum1-1),0],[length*stackNum2+stackMargin2*(stackNum2-1)-thick,width*stackNum1+stackMargin1*(stackNum1-1),0]]
face3 = ent.add_face pt3
face3.reverse!
face3.pushpull thick
face4 = ent.add_face pt4
face4.reverse!
face4.pushpull thick
a=b=0
until a>stackNum1-1
  until b>stackNum2-1
     t = Geom::Transformation.translation [(length+stackMargin2)*b,(width+stackMargin1)*a,2*thick+2.cm]
     inst=ent.add_instance test_panel,t
     b+=1
   end
   b=0
   a+=1
end
save_path = Sketchup.find_support_file "components", ""
comp_def.save_as(save_path + "/frame.skp")
end
def save_bracket1(length,width,dip,thick_stone,thick_bracket,height,stackNum1,stackMargin1,stackNum2,stackMargin2)
mod = Sketchup.active_model
list = mod.definitions    
mat=mod.materials
comp_def = list.add "zhijia"
comp_def.description = "test_bracket"
ent = comp_def.entities 
path=Sketchup.find_support_file "frame.skp","components"
def_list=mod.definitions
test_frame= def_list.load path
beta=dip/180.0
number1=stackNum2%3
number2=stackNum2%4
if number2<=number1
  number=stackNum2/4
  test_number=4  
else
  number=stackNum2/3
  test_number=3
end
if number==0
number=1
end
number.times do |i|
stone_pt1=[[0-thick_stone/2+i*(length*test_number+stackMargin2*(test_number-1)),0,0],[thick_stone/2+i*(length*test_number+stackMargin2*(test_number-1)),0,0],[thick_stone/2+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta),0],[0-thick_stone/2+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta),0]]
stone_face1 = ent.add_face stone_pt1
stone_face1.reverse!
stone_face1.pushpull thick_stone
end
stone_pt2=[[length*stackNum2+stackMargin2*(stackNum2-1)-thick_stone/2,0,0],[length*stackNum2+stackMargin2*(stackNum2-1)+thick_stone/2,0,0],[length*stackNum2+stackMargin2*(stackNum2-1)+thick_stone/2,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta),0],[length*stackNum2+stackMargin2*(stackNum2-1)-thick_stone/2,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta),0]]
stone_face2 = ent.add_face stone_pt2
stone_face2.reverse!
stone_face2.pushpull thick_stone
number.times do |i|
bracket_pt1=[[0+i*(length*test_number+stackMargin2*(test_number-1)),0.1.m,thick_stone],[thick_bracket+i*(length*test_number+stackMargin2*(test_number-1)),0.1.m,thick_stone],[thick_bracket+i*(length*test_number+stackMargin2*(test_number-1)),0.1.m+thick_bracket,thick_stone],[0+i*(length*test_number+stackMargin2*(test_number-1)),0.1.m+thick_bracket,thick_stone]]
bracket_pt2=[[0+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,thick_stone],[thick_bracket+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,thick_stone],[thick_bracket+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_stone],[0+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_stone]]
bracket_pt5=[[0+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,thick_stone],[thick_bracket+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,thick_stone],[thick_bracket+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_stone+thick_bracket*Math.sin(Math::PI*dip/180)],[0+i*(length*test_number+stackMargin2*(test_number-1)),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_stone+thick_bracket*Math.sin(Math::PI*dip/180)]]
bracket_face1 = ent.add_face bracket_pt1
bracket_face1.pushpull (height-thick_stone+0.1.m*Math.tan(Math::PI*beta))
bracket_face2 = ent.add_face bracket_pt2
bracket_face2.pushpull (height-thick_stone+((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket)*Math.tan(Math::PI*beta))
bracket_face5 = ent.add_face bracket_pt5
bracket_face5.pushpull ((height-thick_stone+((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket)*Math.tan(Math::PI*beta)))*Math.cos(Math::PI*dip/180)
end
bracket_pt3=[[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,0.1.m,thick_stone],[length*stackNum2+stackMargin2*(stackNum2-1),0.1.m,thick_stone],[length*stackNum2+stackMargin2*(stackNum2-1),0.1.m+thick_bracket,thick_stone],[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,0.1.m+thick_bracket,thick_stone]]
bracket_pt4=[[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,thick_stone],[length*stackNum2+stackMargin2*(stackNum2-1),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,thick_stone],[length*stackNum2+stackMargin2*(stackNum2-1),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_stone],[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_stone]]
bracket_pt6=[[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,thick_stone],[length*stackNum2+stackMargin2*(stackNum2-1),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,thick_stone],[length*stackNum2+stackMargin2*(stackNum2-1),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_stone+thick_bracket*Math.sin(Math::PI*dip/180)],[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_stone+thick_bracket*Math.sin(Math::PI*dip/180)]]
bracket_face3 = ent.add_face bracket_pt3
bracket_face3.pushpull (height-thick_stone+0.1.m*Math.tan(Math::PI*beta))
bracket_face4 = ent.add_face bracket_pt4
bracket_face4.pushpull (height-thick_stone+((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket)*Math.tan(Math::PI*beta))
bracket_face6 = ent.add_face bracket_pt6
bracket_face6.pushpull ((height-thick_stone+((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket)*Math.tan(Math::PI*beta)))*Math.cos(Math::PI*dip/180)
t_tran = Geom::Transformation.translation [0,0,height]
t_rota = Geom::Transformation.new [0,0,0],[1,0,0],dip.degrees
t=t_tran*t_rota
inst1=ent.add_instance test_frame,t
save_path = Sketchup.find_support_file "components", ""
comp_def.save_as(save_path + "/bracket1.skp")
end
def save_bracket2(length,width,dip,thick_bracket,height,stackNum1,stackMargin1,stackNum2,stackMargin2)
mod = Sketchup.active_model
list = mod.definitions    
mat=mod.materials
comp_def = list.add "zhijia"
comp_def.description = "test_bracket"
ent = comp_def.entities 
path=Sketchup.find_support_file "frame.skp","components"
def_list=mod.definitions
test_frame= def_list.load path
beta=dip/180.0
number=stackNum2/4
if number==0
number=1
end
number.times do |i|
bracket_pt=[(length*4+stackMargin2*3)/2+i*(length*4+stackMargin2*3),((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta))/2,0]
circle=ent.add_circle bracket_pt,[0,0,1],thick_bracket
circle_face = ent.add_face circle
circle_face.reverse!
circle_face.pushpull (height+(((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta))/2-thick_bracket/2)*Math.tan(Math::PI*beta))
end
t_tran = Geom::Transformation.translation [0,0,height]
t_rota = Geom::Transformation.new [0,0,0],[1,0,0],dip.degrees
t=t_tran*t_rota
inst1=ent.add_instance test_frame,t
save_path = Sketchup.find_support_file "components", ""
comp_def.save_as(save_path + "/bracket2.skp")
end
def save_bracket3(length,width,dip,thick_bracket,height,stackNum1,stackMargin1,stackNum2,stackMargin2)
mod = Sketchup.active_model
list = mod.definitions    
mat=mod.materials
comp_def = list.add "zhijia"
comp_def.description = "test_bracket"
ent = comp_def.entities 
path=Sketchup.find_support_file "frame.skp","components"
def_list=mod.definitions
test_frame= def_list.load path
beta=dip/180.0
number=stackNum2/4
if number==0
number=1
end
number.times do |i|
bracket_pt1=[[0+i*(length*4+stackMargin2*3),0.1.m,0],[thick_bracket+i*(length*4+stackMargin2*3),0.1.m,0],[thick_bracket+i*(length*4+stackMargin2*3),0.1.m+thick_bracket,0],[0+i*(length*4+stackMargin2*3),0.1.m+thick_bracket,0]]
bracket_pt2=[[0+i*(length*4+stackMargin2*3),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,0],[thick_bracket+i*(length*4+stackMargin2*3),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,0],[thick_bracket+i*(length*4+stackMargin2*3),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,0],[0+i*(length*4+stackMargin2*3),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,0]]
bracket_pt5=[[0+i*(length*4+stackMargin2*3),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,0],[thick_bracket+i*(length*4+stackMargin2*3),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,0],[thick_bracket+i*(length*4+stackMargin2*3),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_bracket*Math.sin(Math::PI*dip/180)],[0+i*(length*4+stackMargin2*3),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_bracket*Math.sin(Math::PI*dip/180)]]
bracket_face1 = ent.add_face bracket_pt1
bracket_face1.pushpull -(height+0.1.m*Math.tan(Math::PI*beta))
bracket_face2 = ent.add_face bracket_pt2
bracket_face2.pushpull -(height+((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket)*Math.tan(Math::PI*beta))
bracket_face5 = ent.add_face bracket_pt5
bracket_face5.pushpull -((height+((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket)*Math.tan(Math::PI*beta)))*Math.cos(Math::PI*dip/180)
end
bracket_pt3=[[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,0.1.m,0],[length*stackNum2+stackMargin2*(stackNum2-1),0.1.m,0],[length*stackNum2+stackMargin2*(stackNum2-1),0.1.m+thick_bracket,0],[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,0.1.m+thick_bracket,0]]
bracket_pt4=[[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,0],[length*stackNum2+stackMargin2*(stackNum2-1),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,0],[length*stackNum2+stackMargin2*(stackNum2-1),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,0],[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,0]]
bracket_pt6=[[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,0],[length*stackNum2+stackMargin2*(stackNum2-1),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket,0],[length*stackNum2+stackMargin2*(stackNum2-1),(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_bracket*Math.sin(Math::PI*dip/180)],[length*stackNum2+stackMargin2*(stackNum2-1)-thick_bracket,(width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m,thick_bracket*Math.sin(Math::PI*dip/180)]]
bracket_face3 = ent.add_face bracket_pt3
bracket_face3.pushpull -(height+0.1.m*Math.tan(Math::PI*beta))
bracket_face4 = ent.add_face bracket_pt4
bracket_face4.pushpull -(height+((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket)*Math.tan(Math::PI*beta))
bracket_face6 = ent.add_face bracket_pt6
bracket_face6.pushpull -((height+((width*stackNum1+stackMargin1*(stackNum1-1))*Math.cos(Math::PI*beta)-0.1.m-thick_bracket)*Math.tan(Math::PI*beta)))*Math.cos(Math::PI*dip/180)
t_tran = Geom::Transformation.translation [0,0,height]
t_rota = Geom::Transformation.new [0,0,0],[1,0,0],dip.degrees
t=t_tran*t_rota
inst1=ent.add_instance test_frame,t
save_path = Sketchup.find_support_file "components", ""
comp_def.save_as(save_path + "/bracket3.skp")
end
if bracketLayoutType==1
  long=width.m
  wide=length.m
else
  long=length.m
  wide=width.m
end
p save_panel(long,wide,thick_panel)
p save_frame(long,wide,thick_frame,stackNum1,stackMargin1,stackNum2,stackMargin2)
#p save_bracket1(long,wide,dip,thick_stone,thick_bracket1,height,stackNum1,stackMargin1,stackNum2,stackMargin2)
#p save_bracket2(long,wide,dip,thick_bracket2,height,stackNum1,stackMargin1,stackNum2,stackMargin2)
p save_bracket3(long,wide,dip,thick_bracket3,height,stackNum1,stackMargin1,stackNum2,stackMargin2)
#------------------baijb--------------------------
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
def_list=mod.definitions
temp_com = def_list['SolarPV123']
if temp_com
p Sketchup.active_model.definitions['SolarPV123'].count_instances
   def_list.remove(temp_com)
end
#path=Sketchup.find_support_file "bracket1.skp","components"
#path=Sketchup.find_support_file "bracket2.skp","components"
path=Sketchup.find_support_file "bracket3.skp","components"
test_panel= def_list.load path
test_panel.name="SolarPV123"
#------------------baijb--------------------------
solar_long=long*stackNum2+stackMargin2*(stackNum2-1)
solar_wide=wide*stackNum1+stackMargin1*(stackNum1-1)
obj = Sketchup.active_model.selection[0]
inner_loops = obj.loops - [obj.outer_loop]	
# p 'outer loop'
outer_array=[]
outer_array=obj.outer_loop.vertices.collect{|v| v.position}	
# In your original code array.collect returns an array of vertex positions
# but you aren't storing that data anywhere
#p inner_loops.each{|l| l.vertices.collect{|v| v.position}}
# right up here -----  ^
# so let's create a place to store our arrays of vertex positions
inner_array = [] 
# iterate the loops and push the vertex positions array on to the inner_array
inner_loops.each{|l|
  inner_array << l.vertices.collect{|v| v.position}
}
face = Sketchup.active_model.selection[0]
vector_face_norm = face.normal
zenith1=vector_face_norm.angle_between Z_AXIS
zenith1=Math::PI-zenith1
vector1=vector_face_norm 
vector1[2]=0
azimuth1=vector1.angle_between Y_AXIS
if vector1[0]>0
azimuth1=-azimuth1
end
if zenith1==0
azimuth1=0
end
fxj=azimuth1.radians
fxj=fxj.round(1)
qj=zenith1.radians
qj=qj.round(1)
#chan_fq函数定义
theta1=azimuth
if qj>0
theta1=0
end
def chan_fq(edge,num,fxj,qj)
    c_q=[[1,0,0],[0,Math.cos(Math::PI*qj/180),-Math.sin(Math::PI*qj/180)],[0,Math.sin(Math::PI*qj/180),Math.cos(Math::PI*qj/180)]]
    c_f=[[Math.cos(Math::PI*fxj/180),-Math.sin(Math::PI*fxj/180),0],[Math.sin(Math::PI*fxj/180),Math.cos(Math::PI*fxj/180),0],[0,0,1]]
    c=Matrix[c_f[0],c_f[1],c_f[2]]*Matrix[c_q[0],c_q[1],c_q[2]]
    edge_0=Matrix[*edge]
    edge1=edge_0*c
    return edge1
end
#chan_theta函数定义
def chan_theta(edge,num,theta)
    c_t=[[Math.cos(Math::PI*theta/180),-Math.sin(Math::PI*theta/180),0],[Math.sin(Math::PI*theta/180),Math.cos(Math::PI*theta/180),0],[0,0,1]]
    edge_0=Matrix[*edge]
    edge2=edge_0*Matrix[c_t[0],c_t[1],c_t[2]]
    return edge2
end
num=outer_array.length
edge_0=[]
num.times do |i|
edge_0=edge_0+[[outer_array[i][0],outer_array[i][1],outer_array[i][2]]]
end
e_num=edge_0.length
edge_1=chan_fq(edge_0,e_num,fxj,qj)
edge_2=chan_theta(edge_1,e_num,theta1)
edge_3=[]
e_num.times do |i|
edge_3=edge_3+[[edge_2.element(i,0),edge_2.element(i,1),edge_2.element(i,2)]]
end
c_q=[[1,0,0],[0,Math.cos(Math::PI*qj/180),-Math.sin(Math::PI*qj/180)],[0,Math.sin(Math::PI*qj/180),Math.cos(Math::PI*qj/180)]]
c_f=[[Math.cos(Math::PI*fxj/180),-Math.sin(Math::PI*fxj/180),0],[Math.sin(Math::PI*fxj/180),Math.cos(Math::PI*fxj/180),0],[0,0,1]]
c_t=[[Math.cos(Math::PI*theta1/180),-Math.sin(Math::PI*theta1/180),0],[Math.sin(Math::PI*theta1/180),Math.cos(Math::PI*theta1/180),0],[0,0,1]]
#fxj,qj为斜面的倾角，theta为斜面和直线经过fxj和qj变化（chan_fq）之后，直线和y轴的夹角
c = Matrix[c_f[0],c_f[1],c_f[2]]*Matrix[c_q[0],c_q[1],c_q[2]]*Matrix[c_t[0],c_t[1],c_t[2]]
f=1
edge_x0=[]
edge_y0=[]
z1=edge_3[0].z
num.times do |i|
   edge_x0[i]=edge_3[i].x
   edge_x0[i]=edge_x0[i].to_m
   edge_y0[i]=edge_3[i].y
   edge_y0[i]=edge_y0[i].to_m
end
point0=par(edge_x0,edge_y0,outMargin,f)
num.times do |i|
	edge_x0[i]=point0[i][0]
	edge_x0[i]=edge_x0[i].m
	edge_y0[i]=point0[i][1]
	edge_y0[i]=edge_y0[i].m
end
k=0
b=1
y_max=2
y_min=3
edge = Array.new(num){ Array.new(4,0)}
num.times do |i|
  s=(i+1)%num
  edge[i][k]=(edge_y0[s]- edge_y0[i])/(edge_x0[s]- edge_x0[i])
  if  edge[i][k]==0
    edge[i][b]=edge_x0[i] 
  elsif edge[i][k]==1.0/0.0
    edge[i][b]=edge_x0[i]
  elsif edge[i][k]==-1.0/0.0
    edge[i][b]=edge_x0[i]
  else
    edge[i][b]=edge_y0[i]-edge[i][k]*edge_x0[i]
  end
  if edge_y0[i]>=edge_y0[s]
    edge[i][y_max]=edge_y0[i]
    edge[i][y_min]=edge_y0[s]
  else
    edge[i][y_max]=edge_y0[s]
    edge[i][y_min]=edge_y0[i]
  end
end
block_n=inner_array.length
block_num=[]
num1=0
block_n.times do |i|
   block_num[i]=inner_array[i].length
   num1=num1+inner_array[i].length
end
inner_array1=[]
block_n.times do |i|
block_0=[]
block_num[i].times do |j|
  block_0=block_0+[[inner_array[i][j].x,inner_array[i][j].y,inner_array[i][j].z]]
end
b_num=block_0.length
block_1=chan_fq(block_0,b_num,fxj,qj)
block_2=chan_theta(block_1,b_num,theta1)
block_3=[]
b_num.times do |k|
  block_3=block_3+[[block_2.element(k,0),block_2.element(k,1),block_2.element(k,2)]]
end
inner_array1=inner_array1+[block_3]
end
block_x0 = []
block_y0 = []
block_x = []
block_y = []
block_n.times do |i|
   block_num[i].times do |j|
       block_x0[j] =inner_array1[i][j].x
       block_y0[j] =inner_array1[i][j].y
    end
    block_x=block_x+block_x0
    block_y=block_y+block_y0
end
num1.times do |i|
	block_x[i]=block_x[i].to_m
	block_y[i]=block_y[i].to_m
end
ip=0
point1=[]
block_n.times do |i|
ip=ip+block_num[i]
point11=par(block_x[ip-block_num[i]..ip-1],block_y[ip-block_num[i]..ip-1],outMargin,-f)
point1=point1+point11
end
num1.times do |i|
	block_x[i]=point1[i][0]
	block_x[i]=block_x[i].m
	block_y[i]=point1[i][1]
	block_y[i]=block_y[i].m
end
k=0
b=1
y_max=2
y_min=3
block1 =[]
ip=0
x1=[]
y1=[]
block_n.times do |i|
  block = Array.new(block_num[i]){Array.new(4)}
  ip=ip+block_num[i]
  x1=block_x[ip-block_num[i]..ip-1]
  y1=block_y[ip-block_num[i]..ip-1]
  block_num[i].times do |j|
  s=(j+1)%block_num[i]
  block[j][k]=(y1[s]- y1[j])/(x1[s]- x1[j])
  if  block[j][k]==0
    block[j][b]=x1[j] 
  elsif block[j][k]==1.0/0.0
    block[j][b]=x1[j]
  elsif block[j][k]==-1.0/0.0
    block[j][b]=x1[j]
  else
    block[j][b]=y1[j]-block[j][k]*x1[j]
  end
  if y1[j]>=y1[s]
    block[j][y_max]=y1[j]
    block[j][y_min]=y1[s]
  else
    block[j][y_max]=y1[s]
    block[j][y_min]=y1[j]
  end
  end 
  x1=[]
  y1=[]
  block1=block1+block
end
k=0
b=1
y_max=2
y_min=3
way=2
final_num = 0
final_point = []
min_x=edge_x0.min
max_x=edge_x0.max
min_y=edge_y0.min
max_y=edge_y0.max
inst=[]
inst_array=[]
beta=(qj+dip)/180.0
theta=(fxj+theta1)/180.0
if isAlgin==false
  #对齐
  final_num0=0
  final_point0=[]
  xx=[]
  i=min_x
  n=0
  k=1
  until i>max_x
    if k==overhaulPortraitNum
      delta_x=overhaulLongitudinalWidth
      k=1
    else
      delta_x=componentMargin
      k=k+1
    end
    if i+solar_long<max_x
      xx[n]=i
      n=n+1
    end
    i=i+delta_x+solar_long
  end
  xx.length.times do |i|
      xx[i]=xx[i].round(1)
  end
  k=1
  y=min_y
  num_end=0
  until y>max_y
    ex1=get_point(y,num,edge)
	  ex1.length.times do |i|
	    ex1[i]=ex1[i].round(1)
	  end
    ex1=ex1.uniq.sort!
    ex2=get_point(y+solar_wide*Math.cos(Math::PI*((dip-qj)/180.0)),num,edge)
	  ex2.length.times do |i|
      ex2[i]=ex2[i].round(1)
    end
    ex2=ex2.uniq.sort!
    ex=co2(ex1,ex2,1)
    final_bx=[]
    ip=0
    block_n.times do |i|
      ip=ip+block_num[i]
      bx1=get_point(y,block_num[i],block1[ip-block_num[i]..ip-1])
      bx2=get_point(y+solar_wide*Math.cos(Math::PI*((dip-qj)/180.0)),block_num[i],block1[ip-block_num[i]..ip-1])
      bx1.length.times do |j|
	      bx1[j]=bx1[j].round(1)
      end
      bx1=bx1.uniq.sort!
      bx2.length.times do |j|
	      bx2[j]=bx2[j].round(1)
      end
      bx2=bx2.uniq.sort!
      bx=co2(bx1,bx2,-1)
      final_bx=final_bx+bx
      final_bx=final_bx.uniq.sort!
    end
    x=ex+final_bx
    x=x.uniq.sort!
    num_xx=xx.length
    put_i=0
    get_x=[]
    n=0
    num_xx.times do |i|
      if x[2*put_i]==nil
            break
        elsif xx[i]>=x[2*put_i]
          if xx[i]+solar_long<x[2*put_i+1]
            get_x[n]=xx[i]
            n=n+1
            final_num=final_num+1
          else
            put_i=put_i+1
          end
        end
        if put_i>x.length/2
          break
        end
    end
    put_num=get_x.length
    put_num.times do |i|
      inst_array=inst_array+[[get_x[i],y]]
    end
    num_end=num_end+put_num
    if k==overhaulTranNum
      k=1
      y=y+overhaulTranWidth+solar_wide*Math.cos(Math::PI*((dip-qj)/180.0))
    else
      k=k+1
      y=y+componentSpacing+solar_wide*Math.cos(Math::PI*((dip-qj)/180.0))
    end
  end
  zujian_num=inst_array.length
  if zujian_num>=componentNum
    jisuan_num=componentNum
  else
    jisuan_num=zujian_num
  end
  jisuan_num.times do |i|
      test_point1=Matrix[[inst_array[i][0],inst_array[i][1],z1+bottomSpacing]]*(c.inverse)
      test_point2=[test_point1.element(0,0),test_point1.element(0,1),test_point1.element(0,2)]
      t_tran=Geom::Transformation.translation [test_point2[0],test_point2[1],test_point2[2]]
      t_rota = Geom::Transformation.new [0,0,0],[1,0,0],0.degrees
      t_rota1 = Geom::Transformation.new [0,0,0],[0,0,1],(fxj+theta1).degrees
      t=t_tran*t_rota1*t_rota
      inst1=ent.add_instance test_panel,t
     inst=inst+[inst1] 
   end
   num_end=jisuan_num
else
  #非对齐
  final_num0=0
  final_point0=[]
  y_k=1
  y=min_y
  num_end=0
  until y>max_y
    ex1=get_point(y,num,edge)
	  ex1.length.times do |i|
	    ex1[i]=ex1[i].round(1)
	  end
    ex1=ex1.uniq.sort!
    ex2=get_point(y+solar_wide*Math.cos(Math::PI*((dip-qj)/180.0)),num,edge)
	  ex2.length.times do |i|
      ex2[i]=ex2[i].round(1)
    end
    ex2=ex2.uniq.sort!
    ex=co2(ex1,ex2,1)
    final_bx=[]
    ip=0
    block_n.times do |i|
      ip=ip+block_num[i]
      bx1=get_point(y,block_num[i],block1[ip-block_num[i]..ip-1])
      bx2=get_point(y+solar_wide,block_num[i],block1[ip-block_num[i]..ip-1])
      bx1.length.times do |j|
	      bx1[j]=bx1[j].round(1)
      end
      bx1=bx1.uniq.sort!
      bx2.length.times do |j|
	      bx2[j]=bx2[j].round(1)
      end
      bx2=bx2.uniq.sort!
      bx=co2(bx1,bx2,-1)
      final_bx=final_bx+bx
      final_bx=final_bx.uniq.sort!
    end
    x=ex+final_bx
    x=x.uniq.sort!
    q=put_solargai(x,solar_long,overhaulLongitudinalWidth,componentMargin,overhaulPortraitNum)
    put_num=q.length
     put_num.times do |i|
     inst_array=inst_array+[[q[i],y]]
   end
   num_end=num_end+put_num
  if y_k==overhaulTranNum
      y_k=1
      y=y+overhaulTranWidth+solar_wide*Math.cos(Math::PI*((dip-qj)/180.0))
    else
      y_k=y_k+1
      y=y+componentSpacing+solar_wide*Math.cos(Math::PI*((dip-qj)/180.0))
    end
  end
  zujian_num=inst_array.length
  if zujian_num>=componentNum
    jisuan_num=componentNum
  else
    jisuan_num=zujian_num
  end
  jisuan_num.times do |i|
      test_point1=Matrix[[inst_array[i][0],inst_array[i][1],z1+bottomSpacing]]*(c.inverse)
      test_point2=[test_point1.element(0,0),test_point1.element(0,1),test_point1.element(0,2)]
      t_tran=Geom::Transformation.translation [test_point2[0],test_point2[1],test_point2[2]]
      t_rota = Geom::Transformation.new [0,0,0],[1,0,0],(0).degrees
      t_rota1 = Geom::Transformation.new [0,0,0],[0,0,1],(fxj+theta1).degrees
      t=t_tran*t_rota1*t_rota
      inst1=ent.add_instance test_panel,t
     inst=inst+[inst1] 
   end
   num_end=jisuan_num
end
t1 = Time.utc(2020,1,17,7,15,0)          #7:15~16:45  10
t2 = Time.utc(2020,2,16,6,45,0)          #6:45~17:15  12
t3 = Time.utc(2020,3,16,6,8,0)            #6:08~17:52  12
t4 = Time.utc(2020,4,15,5,28,0)          #5:28~18:31  14
t5 = Time.utc(2020,5,15,4,54,0)          #4:54~19:06  16
t6 = Time.utc(2020,6,11,4,37,0)          #4:37~19:23  16
t7 = Time.utc(2020,7,17,4,44,0)          #4:44~19:16  16
t8 = Time.utc(2020,8,16,5,14,0)          #5:14~18:42  14
t9 = Time.utc(2020,9,15,5,53,0)          #5:53~18:07  14
t10 = Time.utc(2020,10,15,6,32,0)      #6:32~17:27  12
t11 = Time.utc(2020,11,14,7,7,0)        #7:07~16:53  10
t12 = Time.utc(2020,12,10,7,23,0)      #7:23~16:36  10
array=[t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12]
array_time=[]
array_time1=array_time2=array_time3=array_time4=array_time5=array_time6=array_time7=array_time8=array_time9=array_time10=array_time11=array_time12=[]
s1=s2=0
10.times do |i|
    array_time1=array_time1+[array[0]+60*60*i]
end
12.times do |i|
    array_time2=array_time2+[array[1]+60*60*i]
end
12.times do |i|
    array_time3=array_time3+[array[2]+60*60*i]
end
14.times do |i|
    array_time4=array_time4+[array[3]+60*60*i]
end
16.times do |i|
    array_time5=array_time5+[array[4]+60*60*i]
end
16.times do |i|
    array_time6=array_time6+[array[5]+60*60*i]
end
16.times do |i|
    array_time7=array_time7+[array[6]+60*60*i]
end
14.times do |i|
    array_time8=array_time8+[array[7]+60*60*i]
end
14.times do |i|
    array_time9=array_time9+[array[8]+60*60*i]
end
12.times do |i|
    array_time10=array_time10+[array[9]+60*60*i]
end
10.times do |i|
    array_time11=array_time11+[array[10]+60*60*i]
end
10.times do |i|
    array_time12=array_time12+[array[11]+60*60*i]
end
array_time=array_time1+array_time2+array_time3+array_time4+array_time5+array_time6+array_time7+array_time8+array_time9+array_time10+array_time11+array_time12
edge_time = Array.new(num_end){Array.new(12){Array.new(24,1)}}
result=[]
num_time=array_time.length
a1=b1=n1=0
num_time.times do |t|
info=mod.shadow_info
info["Displayshadows"]=true
info["DisplayOnGroundPlane"]=true
later=array_time[t]
info["ShadowTime"]=later
info["Latitude"]=31
info["Longitude"]=120
direction_towards_sun = info["SunDirection"]
# raytest Sends a ray into the Model and returns what, if anything, it touches
   num_end.times do |i|
   test_point1=Matrix[[inst_array[i][0],inst_array[i][1],z1+bottomSpacing]]*(c.inverse)
   test_point2=[test_point1.element(0,0),test_point1.element(0,1),test_point1.element(0,2)]
   ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*0+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*0,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*0+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*0,test_point2[2]+height+solar_wide*Math.sin(Math::PI*beta)/4*0+bottomSpacing+0.24.m), direction_towards_sun]
   ignore_hidden_geometry0 = false
   result00 = mod.raytest( ray, ignore_hidden_geometry0 )
   ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*4+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*4,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*4+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*4,test_point2[2]+height+solar_wide*Math.sin(Math::PI*beta)/4*4+bottomSpacing+0.24.m), direction_towards_sun]
   ignore_hidden_geometry1 = false
   result01 = mod.raytest( ray, ignore_hidden_geometry1 )
   if result00==nil
     n1=0
   elsif result01!=nil
     n1=4
   else
   ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*2+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*2,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*2+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*2,test_point2[2]+height+solar_wide*Math.sin(Math::PI*beta)/4*2+bottomSpacing+0.24.m), direction_towards_sun]
   ignore_hidden_geometry = false
   result1 = mod.raytest( ray, ignore_hidden_geometry )
   if result1==nil
     ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*1+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*1,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*1+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*1,test_point2[2]+height+solar_wide*Math.sin(Math::PI*beta)/4*1+bottomSpacing+0.24.m), direction_towards_sun]
     ignore_hidden_geometry = false
     result21 = mod.raytest( ray, ignore_hidden_geometry )
     if result21==nil
       n1=0 
     else
       n1=1
     end
    else
      ray=[Geom::Point3d.new(test_point2[0]+solar_long*Math.cos(Math::PI*theta)/4*3+solar_wide*Math.cos(Math::PI*beta)*Math.sin(Math::PI*-theta)/4*3,test_point2[1]+solar_long*Math.sin(Math::PI*theta)/4*3+solar_wide*Math.cos(Math::PI*beta)*Math.cos(Math::PI*theta)/4*3,test_point2[2]+height+solar_wide*Math.sin(Math::PI*beta)/4*3+bottomSpacing+0.24.m), direction_towards_sun]
     ignore_hidden_geometry = false
     result22 = mod.raytest( ray, ignore_hidden_geometry )
     if result22==nil
       n1=2
     else
       n1=3    
     end
    end
    end
     p=n1/4.0
     p=p.round(2)
     result=result+[p]
     n1=0
   end
end
d=[10,12,12,14,16,16,16,14,14,12,10,10]
a=c=1
until c>d[0]
  until a>num_end
    edge_time[a-1][0][c-1+7]=result[(a-1)+num_end*(c-1)]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[1]
  until a>num_end
    edge_time[a-1][1][c-1+6]=result[(a-1)+num_end*(c-1)+num_end*10]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[2]
  until a>num_end
    edge_time[a-1][2][c-1+6]=result[(a-1)+num_end*(c-1)+num_end*22]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[3]
  until a>num_end
    edge_time[a-1][3][c-1+5]=result[(a-1)+num_end*(c-1)+num_end*34]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[4]
  until a>num_end
    edge_time[a-1][4][c-1+4]=result[(a-1)+num_end*(c-1)+num_end*48]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[5]
  until a>num_end
    edge_time[a-1][5][c-1+4]=result[(a-1)+num_end*(c-1)+num_end*64]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[6]
  until a>num_end
    edge_time[a-1][6][c-1+4]=result[(a-1)+num_end*(c-1)+num_end*80]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[7]
  until a>num_end
    edge_time[a-1][7][c-1+5]=result[(a-1)+num_end*(c-1)+num_end*96]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[8]
  until a>num_end
    edge_time[a-1][8][c-1+5]=result[(a-1)+num_end*(c-1)+num_end*110]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[9]
  until a>num_end
    edge_time[a-1][9][c-1+6]=result[(a-1)+num_end*(c-1)+num_end*124]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[10]
  until a>num_end
    edge_time[a-1][10][c-1+7]=result[(a-1)+num_end*(c-1)+num_end*136]
    a +=1
   end
   a=1
   c +=1
end
a=c=1
until c>d[11]
  until a>num_end
    edge_time[a-1][11][c-1+7]=result[(a-1)+num_end*(c-1)+num_end*146]
    a +=1
   end
   a=1
   c +=1
end
if stackNum1==1&&stackNum2==1
number_end=num_end
else
number_end=num_end*stackNum1*stackNum2
end
bracketNum=num_end
bcBracketNum=0
componentNum=number_end
arry=edge_time
  dialog.execute_script("layoutBracketStats(200,#{bracketNum},#{bcBracketNum},#{componentNum},#{arry})")
if fzaArray==[]
else
 p hide_instance(fzaArray,valve,inst,number_end)
end
else
 UI.messagebox("Please select a face")
 dialog.execute_script("layoutBracketStats(500,'','','','')")
end

                                  }

                              #截图
                              dialog.add_action_callback('screenshot_action'){ |_dlg, projectId|
                                      #UI.messagebox(projectId)
                                      screenshot_function(dialog,projectId);

                                  }

                              #同步数据
                              dialog.add_action_callback('synchronization_action'){ |_dlg, projectId|
                                              #UI.messagebox(projectId)
                                              synchronization_function(dialog,projectId);

                                  }
                             # 关闭对话框
                              dialog.set_on_closed {
                                 $dialog_state = 1
                               }
                     end


             end

         end


end





